const storage = {
  setItem(key, value) {
    if (value !== null && typeof value === 'object') {
      value = JSON.stringify(value);
    }
    
    if (value) {
      try {
        uni.setStorageSync(key, value);
      } catch (e) {
        // error
      }
    }
  },
  getItem(key) {
    try {
      let val = uni.getStorageSync(key);
      if (val === null || val === 'null') {
        return null;
      } else if (val === 'undefined') {
        return undefined;
      } else {
        try {
          return JSON.parse(val)
        } catch (error) {
          // 非json值直接返回
          return val;
        }
      }
    } catch (e) {
      return null;
    }
  },
  removeItem(key) {
    try {
      uni.removeStorageSync(key);
    } catch (e) {
        // error
    }
  }
}

export default storage;